package com.ytt.core.api.controller.teacher;

import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.model.common.SimpleDataDTO;
import com.ytt.core.model.teacher.TeacherDto;
import com.ytt.core.model.teacher.TeacherEditDto;
import com.ytt.core.service.service.ProposalQueryParam;
import com.ytt.core.service.service.teacher.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

@Api(tags = {"教师管理"})
@RestController
@RequestMapping("/api/teacher")
@Validated
public class TeacherController {

    private  final TeacherService teacherService;

    @Autowired
    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @ApiOperation(value = "根据ID查找教师")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('TEACHER_RETRIEVE')")
    public TeacherDto findById(
            @ApiParam(value = "教师ID", required = true) @PathVariable String id) {
        return teacherService.findById(id);
    }

    @ApiOperation(value = "分页查找教师")
    @GetMapping(value = "")
    @PreAuthorize("hasAnyAuthority('TEACHER_RETRIEVE')")
    public PageDataDTO<TeacherDto> findOnePage(
            @ApiParam(value = "页号，从0开始", defaultValue = "0") @RequestParam(value = "page", required = false) @Min(0) Integer page,
            @ApiParam(value = "每页纪录条数", defaultValue = "20") @RequestParam(value = "size", required = false) @Min(1) @Max(100) Integer size,
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(value = "教师名称") @RequestParam(value = "name", required = false) String name) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .build();
        return teacherService.findOnePage(page, size, sort, param);
    }

    @ApiOperation(value = "查找所有教师")
    @GetMapping(value = "/all")
    @PreAuthorize("hasAnyAuthority('TEACHER_RETRIEVE')")
    public List<TeacherDto> findAll(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "教师名称") @RequestParam(value = "name", required = false) String name) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .build();
        return teacherService.findAll(sort,param);
    }



    @ApiOperation(value = "创建教师")
    @PostMapping(value = "")
    @PreAuthorize("hasAnyAuthority('TEACHER_CREATE')")
    public TeacherDto create(@ApiParam(value = "教师信息", required = true) @RequestBody @Validated TeacherEditDto teacherEditDto) {
        return teacherService.create(teacherEditDto);
    }

    @ApiOperation(value = "更新教师")
    @PutMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('TEACHER_UPDATE')")
    public TeacherDto update(@ApiParam(value = "教师ID", required = true) @PathVariable String id,
                             @ApiParam(value = "教师信息", required = true) @RequestBody @Validated TeacherEditDto teacherEditDto) {
        return teacherService.update(id, teacherEditDto);
    }

    @ApiOperation(value = "删除教师")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('TEACHER_DELETE')")
    public void delete(@ApiParam(value = "教师ID", required = true) @PathVariable String id) {
        teacherService.delete(id);
    }


    @ApiOperation(value = "查询教师工号是否可用")
    @GetMapping(value = "/availability")
    @PreAuthorize("hasAnyAuthority('TEACHER_RETRIEVE')")
    public SimpleDataDTO<Boolean> checkStudentIdAvailability(
            @ApiParam(value = "教师ID, 新增用户时检查账号可用性可不填") @RequestParam(value = "id", required = false) String id,
            @ApiParam(value = "工号", required = true) @RequestParam(value = "job_number") String job_number) {
        return teacherService.checkJobNumberAvailability(id, job_number);
    }
}
